<?php
/**
 * User: 泉啊泉
 * Date: 2020/8/20 0020
 * Time: 下午 7:13
 */
require_once "./lib/DB.php";

//获取数据
$data = $_POST;
if (empty($data)){
    echo json_encode(["code"=>"404","msg"=>"参数不能为空"]);
}
$date_time = date("Y-m-d H:i:s",time());
$userObj = new DB();
//连接redis
$redis = new Redis();
$redis->connect("127.0.0.1",6379);

switch ($data["option"]){
    case "register":
        //验证数据
        $dataArr = ["name"=>"登录名","nickname"=>"昵称","password"=>"密码","repassword"=>"验证密码","info"=>"简介"];
        foreach($dataArr as $key => $value){
            if (empty($data[$key])){
                echo json_encode(["code"=>"404","msg"=>"{$value}不能为空"]);die;
            }
        }
        if ($data["password"] != $data["repassword"]) {
            echo json_encode(["code"=>"404","msg"=>"两次密码不同，请重新输入"]);die;
        }

        $queryResult = $userObj->query("user");
        $nameArr = array_column($queryResult,"name");
        if (in_array($data["name"],$nameArr)) {
            echo json_encode(["code"=>"404","msg"=>"该用户已存在，请重新输入"]);die;
        }

        //处理数据
        unset($data["repassword"],$data["option"]);
        $data["password"] = password_hash($data["password"],1);
        $data["create_time"] = $date_time;
        $insResult = $userObj->insert("user",$data);

        //返回结果
        echo json_encode($insResult);
    break;
    case "login":
        //验证数据
        $dataArr = ["name"=>"登录名","password"=>"密码"];
        foreach($dataArr as $key => $value){
            if (empty($data[$key])){
                echo json_encode(["code"=>"404","msg"=>"{$value}不能为空"]);die;
            }
        }

        $getRedis = $redis->get("login");
        if (!$getRedis){
            $count = 1;
        }else if($getRedis >= 5){
            echo json_encode(["code"=>"404","msg"=>"您的错误次数已达上限,请次日再试"]);die;
        }else {
            $count = $getRedis + 1;
        }


        $userObj->where("name",$data["name"]);
        $queryResult = $userObj->query("user");

        if (count($queryResult) <= 0){
            echo json_encode(["code"=>"404","msg"=>"该用户不存在，请重新输入"]);die;
        }

        if (!password_verify($data["password"],$queryResult[0]["password"])) {
            $redis->set("login",$count);
            $expireTime = mktime(23, 59, 59, date("m"), date("d"), date("Y"));
            //设置键的过期时间
            $redis->expireAt("login", $expireTime);
            echo json_encode(["code"=>"404","msg"=>"密码错误，请重新输入"]);die;
        }
        setcookie("user",json_encode($queryResult),time()+60000);
        echo json_encode(["code"=>"1","msg"=>"登录成功"]);
    break;
    case "addMsg":
        $userData = json_decode($_COOKIE["user"],true)[0];//当前用户数据

        $getRedis = $redis->get($userData["id"]);
        if (!$getRedis){
            $count = 1;
        }else if($getRedis >= 1000){
            echo json_encode(["code"=>"404","msg"=>"您发帖次数已达上限"]);die;
        }else {
            $count = $getRedis + 1;
        }

        //验证数据
        $dataArr = ["title"=>"标题","content"=>"内容"];
        foreach($dataArr as $key => $value){
            if (empty($data[$key])){
                echo json_encode(["code"=>"404","msg"=>"{$value}不能为空"]);die;
            }
        }

        unset($data["option"]);
        $data["create_time"] = $date_time;
        $data["user_name"] = $userData["nickname"];

        $insResult = $userObj->insert("info",$data);

        if ($insResult["code"] != 1){
            echo json_encode(["code"=>"404","msg"=>"增加失败"]);die;
        }
        //存redis
        $redis->set($userData["id"],$count);
        //返回结果
        echo json_encode($insResult);
    break;
    default:
        echo json_encode(["code"=>"403","msg"=>"非法请求"]);
    break;
}
